<?php
require 'DualIterator.php';
class SugarArrayIterator extends ArrayIterator{
	public function __construct(){
		
	}
}

class KeyValueIterator extends DualIterator{
	public function __construct( $iterator, $flags ){
		parent::__construct( new KeyIterator(clone $iterator), clone $iterator, $flags );
	}
/*	public function next(){
		parent::getLHS()->next();
	}*/
}

class DecoratingKeyValueIterator extends DualIterator{
	public function __construct( $iterator, $decorator, $flags = parent::DEFAULT_FLAGS ){
		parent::__construct(
			new DecoratingIterator( new KeyIterator(clone $iterator), $decorator, $flags ),
			new DecoratingIterator( clone$iterator, $decorator ),
			$flags
		);
	}
	/*public function next(){
		parent::getLHS()->next();
	}*/
}

class KeyIterator extends IteratorIterator{
	function current(){
		return $this->key();
	}
}

class DecoratingIterator extends IteratorIterator{
	private $decorator;
	private $current_key;
	function __construct( $childIterator, $decorator ){
		$this->decorator = $decorator;
		parent::__construct( $childIterator );
	}
	function current(){
		return $this->decorator->decorate( parent::current() );
	}
}

class SugarDualIteratorIterator implements Iterator{
	private $dualIterator;
	private $current_key;
	public function __construct( $dualIterator ){
		$this->dualIterator = $dualIterator;
	}
	public function rewind(){
		$this->dualIterator->rewind();
		$this->_update_key();
	}
	public function next(){
		$this->dualIterator->next();
		$this->_update_key();
	}
	public function current(){
		$this->dualIterator->setFlags( DualIterator::CURRENT_RHS );
		return $this->dualIterator->current();
	}
	public function valid(){
		return $this->dualIterator->valid();
	}
	private function _update_key(){
		$this->dualIterator->setFlags( DualIterator::CURRENT_ARRAY );
		list( $left, $right ) = $this->dualIterator->current();
		$this->current_key = $left;
	}
	public final function key(){
		throw new CV_OutputFilter_Exception( "Regular access to keys not allowed. Please use the 'key_as' method instead." );
	}
	public function key_as( &$key ){
		$this->current_key = &$key;
		return $this;
	}
}
